package org.example.six;

import com.rabbitmq.client.*;
import org.example.utils.RabbitMqUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ReceiveLogsDirect01 {
    public static final String EXCHANGE_NAME = "direct";

    public static void main(String[] args) throws IOException, TimeoutException {
        String queueName = "console";
        String routingKey = "info";
        Channel channel = RabbitMqUtil.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //声明一个队列
        channel.queueDeclare(queueName,false,false,false,null);
        //绑定队列和交换机
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey);
        DeliverCallback deliverCallback = (String message, Delivery delivery) -> {
            System.out.println("消息接收到了");
            System.out.println(new String(delivery.getBody()));
        };
        CancelCallback cancelCallback = message -> {
            System.out.println("消息取消了");
            System.out.println(message);
        };
        //消费消息
        channel.basicConsume(queueName,true,deliverCallback,cancelCallback);
    }
}
